/**
 * 
 */
package com.p2p.front.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.p2p.front.dal.dao.UserInfoDao;
import com.p2p.front.dal.model.UserInfo;
import com.p2p.front.service.UserInfoService;
import com.p2p.front.vo.UserInfoVO;
import com.p2p.user.enums.UserInfoName;

/**
 * @author ruiqi
 *
 */
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
	
	private final Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);
	
	@Resource
	UserInfoDao userInfoDao;


	@Override
	public UserInfoVO findByEnumNames(String userId,
			List<String> queryNameList) {
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		userInfo.setQueryNameList(queryNameList);
		List<UserInfo> list = userInfoDao.findByNames(userInfo);
		Map<String,String> infoMap = new HashMap<String,String>();
		for(UserInfo info:list){
			infoMap.put(info.getInfoName(), info.getInfoValue());
		}
		UserInfoVO userInfoVO = new UserInfoVO();
		userInfoVO.setUserId(userId);
		userInfoVO.setInfoMap(infoMap);
		return userInfoVO;
	}


	@Override
	public UserInfoVO findByNameEnums(String userId, List<UserInfoName> queryEmunList) {
		List<String> queryNameList = new ArrayList<String>();
		for(UserInfoName name:queryEmunList){
			queryNameList.add(name.name());
		}
		return this.findByEnumNames(userId, queryNameList);
	}


	@Override
	public void batchSave(String userId, List<UserInfo> userInfoList) {
		for(UserInfo userInfo:userInfoList){
			userInfo.setUserId(userId);
		}
		userInfoDao.batchSave(userInfoList);		
	}


	@Override
	public void batchSave(String userId, TreeMap<UserInfoName, String> infoValueMap) {
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		
		for(Map.Entry<UserInfoName, String> entry:infoValueMap.entrySet()){
			UserInfo userInfo = new UserInfo();
			userInfo.setUserId(userId);
			userInfo.setInfoName(entry.getKey().name());
			userInfo.setInfoValue(entry.getValue());
			userInfoList.add(userInfo);
		}
		userInfoDao.batchSave(userInfoList);
	}


	@Override
	public void batchSave(UserInfoVO userInfoVO) {
		String userId = userInfoVO.getUserId();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for(Map.Entry<String, String> entry:userInfoVO.getInfoMap().entrySet()){
			UserInfo userInfo = new UserInfo();
			userInfo.setUserId(userId);
			userInfo.setInfoName(entry.getKey());
			userInfo.setInfoValue(entry.getValue());
			userInfoList.add(userInfo);
		}
		userInfoDao.batchSave(userInfoList);
	}


	@Override
	public Map<String, Object> getOne(String userId,UserInfoName infoName) {
		UserInfo userInfo  = new UserInfo();
		userInfo.setUserId(userId);
		userInfo.setInfoName(infoName.name());
		return userInfoDao.getOne(userInfo);
	}
	
	public void updateOne(String userId,UserInfoName userInfoName,String infoValue){
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		userInfo.setInfoName(userInfoName.name());
		userInfo.setInfoValue(infoValue);
		userInfoList.add(userInfo);
		userInfoDao.batchSave(userInfoList);
	}
	
	public void updateBorrower(String userId,String auth){
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		userInfo.setInfoName(UserInfoName.BORROWER_AUTH.name());
		userInfo.setInfoValue(auth);
		userInfoList.add(userInfo);
		userInfoDao.batchSave(userInfoList);
	}


}
